package com.jmodata.zxu.controller.test;

import com.jmodata.zxu.DBRelevant.QueryParam;
import com.jmodata.zxu.model.PhoneReturnJson;
import com.jmodata.zxu.pojo.user.MapperUser;
import com.jmodata.zxu.service.test.MapperUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("api")
public class MapperUserController {

    @Autowired(required = false)
    private MapperUserService mapperUserService;

    @ResponseBody
    @RequestMapping("addMapperUser")
    public PhoneReturnJson addMapperUser(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        if (StringUtils.isNotBlank(name) && StringUtils.isNotBlank(phone)) {
            MapperUser mp = new MapperUser();
            mp.setName(name);
            mp.setPhone(phone);
            return mapperUserService.insertMapperUser(mp);
        }else {
            return new PhoneReturnJson(false, "参数不能为空！", null);
        }
    }

    @ResponseBody
    @RequestMapping("edtMapperUser")
    public PhoneReturnJson edtMapperUser(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        int id = ServletRequestUtils.getIntParameter(request,"id");
        MapperUser mp = new MapperUser();
        mp.setId((long) id);
        mp.setName(name);
        mp.setPhone(phone);
        return mapperUserService.updateMapperUser(mp);
    }

    @ResponseBody
    @RequestMapping("delMapperUser")
    public PhoneReturnJson delMapperUser(HttpServletRequest request, HttpServletResponse response)throws Exception{
        int id = 11;
        return mapperUserService.delMapperUser(id);
    }

    @ResponseBody
    @RequestMapping("mapperUserAll")
    public PhoneReturnJson mapperUserAll(HttpServletRequest request, HttpServletResponse response)throws Exception{
        QueryParam param = new QueryParam();
        return mapperUserService.mapperUserAll(param);
    }

    @ResponseBody
    @RequestMapping("mapperUserList")
    public PhoneReturnJson mapperUserList(HttpServletRequest request, HttpServletResponse response)throws Exception{
        QueryParam param = new QueryParam();
        param.setPageNum(1);//初始当前页，从1开始。
        param.setPageSize(20);
        param.put("name","名字");
        param.addCondition("phone", "like", "%1%");
        return mapperUserService.mapperUserList(param);
    }
}
